package com.qdairlines.service.user.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.qdairlines.dao.user.RoleMapper;
import com.qdairlines.entity.user.Role;
import com.qdairlines.entity.user.RolePermission;
import com.qdairlines.entity.user.UserRole;
import com.qdairlines.entity.user.UserRoleDto;
import com.qdairlines.service.base.impl.BaseServiceImpl;
import com.qdairlines.service.user.RolePermissionService;
import com.qdairlines.service.user.RoleService;
import com.qdairlines.service.user.UserRoleService;

@Service
public class RoleServiceImpl extends BaseServiceImpl<Role,Integer> implements RoleService {

    @Autowired
    private RoleMapper roleMapper;
    
    @Autowired
	private RolePermissionService rolePermissionService;
    @Autowired
    private UserRoleService userRoleService;
    
    @Autowired
	public void setBaseMapper() {
		super.setBaseMapper(roleMapper);
	}

	@Override
	public List<Role> listRoleByWorkcode(String workcode) {
		return roleMapper.selectRolesByWorkcode(workcode);
	}

	@Override
	public List<Role> listRoleByPermissionCode(String permissionCode) {
		// TODO Auto-generated method stub
		return roleMapper.selectRolesByPermissionCode(permissionCode);
	}

	@Override
	public String getRoleNamesByPermissionCode(String permissionCode) {
		List<Role> list = this.listRoleByPermissionCode(permissionCode);
		StringBuffer sb = new StringBuffer();
		if(list != null && list.size()>0){
			for(Role r : list){
				sb.append(r.getRoleName());
				sb.append(",");
			}
			sb.deleteCharAt(sb.length()-1);
			return sb.toString();
		}else{
			return null;
		}
	
	}

	@Override
	public List<String> listRoleCodeByWorkcode(String workcode) {
		List<Role> list = this.listRoleByWorkcode(workcode);
		if(list !=null && list.size()>0){
			List<String> listRoleName = new ArrayList<String>();
			for(Role r : list){
				listRoleName.add(r.getRoleCode());
			}
			return listRoleName;
		}
		return null;
	}

	@Override
	public List<UserRoleDto> listUserRoleDtoByWorkcode(String workcode,String isRelated) {
		return roleMapper.listUserRoleDtoByWorkcode(workcode,isRelated);
	}


	@Override
	public Role getRoleByRoleCode(String roleCode) {
		return roleMapper.getRoleByRoleCode(roleCode);
	}


	@Override
	public Role getRoleByRoleName(String roleName) {
		return roleMapper.getRoleByRoleName(roleName);
	}
	
	@Override
	@Transactional
	public int deleteByPrimaryKey(Integer roleId){
		//删除角色权限
		RolePermission rolePermission = new RolePermission();
		rolePermission.setRoleId(roleId);
		List<RolePermission> rplist = rolePermissionService.listData(rolePermission);
		if(rplist !=null && rplist.size()>0){
			for(RolePermission p : rplist){
				rolePermissionService.deleteByPrimaryKey(p.getId());
			}
		}
		
		//删除人员角色
		UserRole userRole = new UserRole();
		userRole.setRoleId(roleId);
		List<UserRole> urList = userRoleService.listData(userRole);
		if(urList !=null && urList.size()>0){
			for(UserRole ur : urList){
				userRoleService.deleteByPrimaryKey(ur.getId());
			}
		}
		
		return roleMapper.deleteByPrimaryKey(roleId);
	}
   
}
